//
//  FPBankCardManageController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/3/13.
//

#import "FPBankCardManageController.h"
#import "FPBankCardModel.h"
#import "FPBankCardDetailCell.h"
#import "FPBankCardDetailController.h"
#import "FPAddNewBankCardController.h"
#import "FPBankCardDetailFooterView.h"

@interface FPBankCardManageController () <UITableViewDelegate , UITableViewDataSource , DZNEmptyDataSetSource , DZNEmptyDataSetDelegate>

@property (nonatomic , strong) FPBankCardDetailFooterView *footerView;

@end

@implementation FPBankCardManageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getBankCardList) name:FPBankCardAddSuccessNotification object:nil];
}

- (void)initUI {
    
    [self setTitle:@"我的银行卡" style:(XYCustomNavigationStyleWhite) hiddenBackBtn:NO];
    
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.frame = CGRectMake(0.f, UIDevice.navigationFullHeight, SCREEN_WIDTH, SCREEN_HEIGHT - UIDevice.navigationFullHeight - 44.f - UIDevice.safeDistanceBottom - 12.f);
    
    self.footerView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPBankCardDetailFooterView.class) owner:nil options:nil].lastObject;
    [self.view addSubview:self.footerView];
    self.footerView.frame = CGRectMake(0.f, SCREEN_HEIGHT - UIDevice.safeDistanceBottom - 44.f - 12.f, SCREEN_WIDTH, 44.f);
    
    [self.footerView.addBtn handleViewControlEvents:UIControlEventTouchUpInside withBlock:^{
       
        [self addBankCardAction];
    }];
    
    self.tableView.rowHeight = 160.f;
    
    [self getBankCardList];
}


- (void)getBankCardList {
    
    [MBManager showLoading];
    [FPBankCardModel getCardListSuccess:^(NSInteger code, NSString * _Nonnull message, NSArray * _Nonnull list) {
        [MBManager hideLoading];
        
        [self.dataSource removeAllObjects];
        
        // 刷新银行卡列表
        if (list.count == 0) {
            
            self.footerView.hidden = YES;
        }
        else {
            
            self.footerView.hidden = NO;
            
            for (FPBankCardModel *model in list) {
                
                [self.dataSource addObject:model];
            }
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        [self showError:error];
    }];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPBankCardDetailCell *cell;
    
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPBankCardDetailCell.class) owner:nil options:nil].lastObject;
    }
    
    FPBankCardModel *model = self.dataSource[indexPath.row];
//    NSLog(@"%@",[MAIN_IMAGE_URL stringByAppendingString:model.imagePath]);
    if (model.imagePath.length) {
     
        [cell.logoImgView sd_setImageWithURL:[NSURL URLWithString:[FPUserModel.globalHeadURL stringByAppendingString:model.imagePath]] placeholderImage:IMG(@"icon_bank_card_default")];
    }
    else {
        
        cell.logoImgView.image = IMG(@"icon_bank_card_default");
    }
    
    cell.nameLab.text = model.bankName;
    cell.lab4.text = [model.bankCard substringFromIndex:model.bankCard.length - 4];
    
    switch (model.colorCode.integerValue) {

        case 1:
            
            // 红
            cell.backImgView.image = IMG(@"bg_bank_card_red");
            break;
            
        case 2:
            
            // 蓝
            cell.backImgView.image = IMG(@"bg_bank_card_blue");
            break;
            
        case 3:
            
            // 黄
            cell.backImgView.image = IMG(@"bg_bank_card_yellow");
            break;
            
        case 4:
            
            // 绿
            cell.backImgView.image = IMG(@"bg_bank_card_green");
            break;
            
        default:
            
            cell.backImgView.image = IMG(@"bg_bank_card_blue");
            break;
    }
    
    return cell;
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    return IMG(@"bg_nodata");
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {

    return - 80.f;
}


- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {

    NSString *text = @"去添加银行卡";
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    
    [NSString stringToAttribute:attStr font:FONT(15.f) range:NSMakeRange(0, text.length)];
    [NSString stringToAttribute:attStr color:MAIN_BLUE_COLOR range:NSMakeRange(0, text.length)];
    
    return attStr;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    
    [self addBankCardAction];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPBankCardModel *model = self.dataSource[indexPath.row];
    
    FPBankCardDetailController *vc = [[FPBankCardDetailController alloc] init];
    
    vc.model = model;
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)addBankCardAction {
    
    FPAddNewBankCardController *vc = [[FPAddNewBankCardController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
    WeakSelf;
    vc.bankCardBlock = ^() {
        
        [weakSelf getBankCardList];
    };
}


@end
